home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-06-06 | 6.7 KB | 304 lines | [TEXT/CWIE] |
- // scrapcolor.c
- //
- // Functions and private types to support the scrap color format.
- // Version 1.0
- // Written by Mark Womack, June 1996.
- //
- // The code and clipboard format are placed in the public domain
- // for the benefit of all. It is hoped that it will become a
- // standard used by many applications, commercial and shareware,
- // to transfer color information effectively.
- //
- // The author takes no responsibility damages rendered caused by bugs
- // or defects lurking in this code. If you find a bug, please use the
- // email address in the accompnaying read me file to contact him.
-
- // includes
- //
- #include "scrapcolor.h"
- #include <Picker.h>
-
- // private types
- //
- typedef struct {
- short model;
- ColorData colorData;
- }ScrapColorData;
-
- // private defines
- //
-
- // the format scrap tag
- #define kScrapColorType 'sCLR'
-
- // the current format version
- #define kFormatVersion 0x0001
-
- // function implementations
- //
-
- // get single color from scrap in model it is stored as
- pascal OSErr GetColorFromScrap(ColorData* theColor, short* theModel)
- {
- Handle scrapDataHdl;
- Ptr thePtr;
- unsigned short version;
- unsigned short numColors;
- long size;
- long offset;
-
- scrapDataHdl = NewHandle(0);
- if (scrapDataHdl == NULL)
- return memFullErr;
-
- // try to get the scrap data
- size = GetScrap(scrapDataHdl, kScrapColorType,&offset);
-
- // error returned by GetScrap
- if (size < 0)
- {
- if (scrapDataHdl)
- DisposeHandle(scrapDataHdl);
- return size;
- }
- // nil handle
- else if (scrapDataHdl == NULL)
- {
- return memFullErr;
- }
- // handle not the right size, so must be memFull
- else if (GetHandleSize(scrapDataHdl) != size)
- {
- DisposeHandle(scrapDataHdl);
- return memFullErr;
- }
- // there aren't any colors in the structure
- else if ((*(short*)(*scrapDataHdl)) == 0)
- {
- DisposeHandle(scrapDataHdl);
- return noTypeErr;
- }
-
- // lock this puppy so it doesn't move
- HLock(scrapDataHdl);
-
- thePtr = *scrapDataHdl;
-
- // get and check the version number
- version = *((unsigned short*)thePtr);
- thePtr += sizeof(unsigned short);
-
- // unknown format
- if (version > kFormatVersion)
- {
- HUnlock(scrapDataHdl);
- DisposeHandle(scrapDataHdl);
- return noTypeErr;
- }
-
- // get the number of colors
- numColors = *((unsigned short*)thePtr);
- thePtr += sizeof(unsigned short);
-
- // get the color model
- *theModel = ((ScrapColorData*)thePtr)->model;
-
- // get the color data
- *theColor = ((ScrapColorData*)thePtr)->colorData;
-
- // throw away the scrap data
- HUnlock(scrapDataHdl);
- DisposeHandle(scrapDataHdl);
-
- return noErr;
- }
-
- // get single color from scrap in the RGB model
- pascal OSErr GetRGBColorFromScrap(RGBColor* theColor)
- {
- ColorData colorData;
- short colorModel;
- OSErr err;
-
- err = GetColorFromScrap(&colorData,&colorModel);
- if (err)
- return err;
-
- // convert the color info if we have to
- return ConvertColor(&colorData,colorModel,(ColorData*)theColor,kRGBModel);
- }
-
- // get single color from scrap in the HSL model
- pascal OSErr GetHSLColorFromScrap(HSLColor* theColor)
- {
- ColorData colorData;
- short colorModel;
- OSErr err;
-
- err = GetColorFromScrap(&colorData,&colorModel);
- if (err)
- return err;
-
- // convert the color info if we have to
- return ConvertColor(&colorData,colorModel,(ColorData*)theColor,kHSLModel);
- }
-
- // get single color from scrap in the HSV model
- pascal OSErr GetHSVColorFromScrap(HSVColor* theColor)
- {
- ColorData colorData;
- short colorModel;
- OSErr err;
-
- err = GetColorFromScrap(&colorData,&colorModel);
- if (err)
- return err;
-
- // convert the color info if we have to
- return ConvertColor(&colorData,colorModel,(ColorData*)theColor,kHSVModel);
- }
-
- // get single color from scrap in the CMY model
- pascal OSErr GetCMYColorFromScrap(CMYColor* theColor)
- {
- ColorData colorData;
- short colorModel;
- OSErr err;
-
- err = GetColorFromScrap(&colorData,&colorModel);
- if (err)
- return err;
-
- // convert the color info if we have to
- return ConvertColor(&colorData,colorModel,(ColorData*)theColor,kCMYModel);
- }
-
- // put single color into scrap in given color model (no conversion)
- pascal OSErr PutColorIntoScrap(const ColorData* colorData, const short colorModel)
- {
- Handle theScrapHdl;
- Ptr thePtr;
- ScrapColorData theScrapColor;
- OSErr theErr;
-
- // create a handle to put the data into
- theScrapHdl = NewHandle(sizeof(unsigned short)*2 + sizeof(ScrapColorData));
- if (!theScrapHdl)
- return memFullErr;
-
- // lock it so it won't move
- HLock(theScrapHdl);
-
- // put the data into the structure
- theScrapColor.model = colorModel;
- theScrapColor.colorData = *colorData;
-
- // set up the pointer that will move
- thePtr = *theScrapHdl;
-
- // put in the format version
- *((unsigned short*)thePtr) = kFormatVersion;
- thePtr += sizeof(unsigned short);
-
- // there is only one color in the handle
- *((unsigned short*)thePtr) = 1;
- thePtr += sizeof(unsigned short);
-
- // store the first color in the handle
- *((ScrapColorData*)thePtr) = theScrapColor;
-
- // put the data into the scrap
- theErr = PutScrap(GetHandleSize(theScrapHdl),kScrapColorType,(Ptr)*theScrapHdl);
-
- // unlock and dispose of the scrap handle
- HUnlock(theScrapHdl);
- DisposeHandle(theScrapHdl);
-
- return theErr;
- }
-
- // put single RGB color into the scrap
- pascal OSErr PutRGBColorIntoScrap(const RGBColor* theColor)
- {
- return PutColorIntoScrap((ColorData*)theColor, kRGBModel);
- }
-
- // put single HSL color into the scrap
- pascal OSErr PutHSLColorIntoScrap(const HSLColor* theColor)
- {
- return PutColorIntoScrap((ColorData*)theColor, kHSLModel);
- }
-
- // put single HSV color into the scrap
- pascal OSErr PutHSVColorIntoScrap(const HSVColor* theColor)
- {
- return PutColorIntoScrap((ColorData*)theColor, kHSVModel);
- }
-
- // put single CMY color into the scrap
- pascal OSErr PutCMYColorIntoScrap(const CMYColor* theColor)
- {
- return PutColorIntoScrap((ColorData*)theColor, kCMYModel);
- }
-
- // convert from one color model to another
- pascal OSErr ConvertColor(const ColorData* oldColor, const short oldModel, ColorData* newColor, const short newModel)
- {
- RGBColor theRGBColor;
-
- // if already in desired model, just return
- if (oldModel == newModel)
- {
- *newColor = *oldColor;
- return noErr;
- }
-
- // convert all models to RGB
- switch(oldModel)
- {
- case kRGBModel:
- theRGBColor = *((RGBColor*)oldColor);
- break;
-
- case kHSVModel:
- HSV2RGB((HSVColor*)oldColor,&theRGBColor);
- break;
-
- case kHSLModel:
- HSL2RGB((HSLColor*)oldColor,&theRGBColor);
- break;
-
- case kCMYModel:
- CMY2RGB((CMYColor*)oldColor,&theRGBColor);
- break;
-
- default:
- return noTypeErr;
- }
-
- // now convert the RGB to the desired model
- switch(newModel)
- {
- case kRGBModel:
- *((RGBColor*)newColor) = theRGBColor;
- break;
-
- case kHSVModel:
- RGB2HSV(&theRGBColor,(HSVColor*)newColor);
- break;
-
- case kHSLModel:
- RGB2HSL(&theRGBColor,(HSLColor*)newColor);
- break;
-
- case kCMYModel:
- RGB2CMY(&theRGBColor,(CMYColor*)newColor);
- break;
-
- default:
- return noTypeErr;
- }
-
- return noErr;
- }
-